package com.gancp.common.security;

import com.gancp.utills.Md5Utils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

/**
 * Created by GCP on 2018/5/27.
 */
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        // info 来自MyRealm的doGetAuthenticationInfo处理过的数据
        // 同理  token来自MyRealm的doGetAuthorizationInfo添加的授权信息
        try {
            UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
            String password = String.valueOf( usernamePasswordToken.getPassword() );
            // 加密後的密碼數據
            Object tokenCrendentials = Md5Utils.enctyptCode( password.getBytes( "utf-8" ) );
            //
            Object accountCrendentials = getCredentials( info );
            // 比较数据是相同
            return equals( tokenCrendentials, accountCrendentials );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
